%%%%%%%%
%%% CALIBRATION FOR THE CZECH REPUBLIC
%%%%%%%%
function [m,p,mss] = readmodel(filter);

%% Filtration on/off
% filter = true - Kalman filter ON
% filter = false - Kalman filter OFF
p.filter = filter;

%% Typical and specific parameter values be used in calibrations

%% 1. Aggregate demand equation (the IS curve)
% lgdp_gap = a1*lgdp_gap{-1} - a2*mci + a3*lx_gdp_gap + e_lgdp_gap;

%% Real monetary conditions index (mci)
% mci = a4*rr_gap + (1-a4)*(-lz_gap);

%output persistence;
%a1 varies between 0.1 (extremely flexible) and 0.95(extremely persistent)
p.a1 = 0.8;

%policy passthrough (the impact of monetary policy on real economy); 
%a2 varies between 0.1 (low impact) to 0.5 (strong impact)
p.a2 = 0.3;

%the impact of external demand on domestic output; 
%a3 varies between 0.1 and 0.7
p.a3 = 0.5;

%the weight of the real interest rate and real exchange rate gaps in MCI;
%a4 varies from 0.3 to 0.8
p.a4 = 0.7;

%persistence in credit premium 
p.a5 = 0.7;

%% 2. Aggregate supply equation (the Phillips curve)
% dot_cpi_x =  b1*dot_cpi_x{-1} + (1-b1)*dot_cpi{+1} + b2*rmc + e_dot_cpi;

%% Real marginal cost (rmc)
% rmc = b3*lgdp_gap + (1-b3)*lz_gap;

% inflation persistence; 
% b1 varies between 0.4 (low persistence) to 0.9 (high persistence)
p.b1 = 0.7;

% policy passthrough (the impact of rmc on inflation); 
% b2 varies between 0.1 (a flat Phillips curve and a high sacrifice ratio) 
% to 0.5 (a steep Phillips curve and a low sacrifice ratio)
p.b2 = 0.2;

% the ratio of imported goods in firms' marginal costs (1-b3); 
% b3 varies between 0.9 for a closed economy to 0.5 for an open economy
p.b3 = 0.7;

%% Food prices
% dot_cpi_food = b21*dot_cpi_food{-1} + (1-b21)*dot_cpi{+1} + b22*(b23*lz_food_gap + (1-b23)*lgdp_gap) + e_dot_cpi_food

% persistence; 
% b21 varies between 0.1 (low persistence) to 0.9 (high persistence)
p.b21 = 0.1;

% passthrough (the impact of world food prices and the business cycle on food prices); 
% b22 varies between 0.1 (low passthrough) 
% to 0.5 (high passthrough)
p.b22 = 0.5;

% the impact of world food prices and the business cycle on food prices; 
% b3 is usually high, e.g. 0.9 - 0.8 with limited impact of the business
% cycle on food prices
p.b23 = 0.8;

%% Oil prices
% dot_cpi_oil = b31*dot_cpi_oil{-1} + (1-b31-b32)*dot_cpi{+1} + b32*(dot_woil + (dot_s + dot_s_cross) + dot_z_eq) + e_dot_cpi_oil;

% persistence; 
% b31 varies between 0.1 (low persistence) to 0.9 (high persistence)
p.b31 = 0.5;

% passthrough (the impact of world oil prices and the exchange rate on oil prices); 
% b32 varies between 0.1 (low passthrough) 
% to 0.5 (high passthrough)
p.b32 = 0.4;

%% Headline inflation
% dot_cpi = W_oil*dot_cpi_oil + W_food*dot_cpi_food + (1-W_oil-W_food)*dot_cpi_x;

% weight of food prices in the CPI basket
p.W_food = 0.24646;

%weight of oil prices in the CPI basket
p.W_oil = 0.028742;

%% 3. Uncovered Interest Rate Parity (UIP)

%% A. UIP with a backward-looking element ("persistent" exchange rate)
% ls = (1-e1)*ls{+1} + e1*(ls{-1} + 2/4*(target - ss_dot_x_cpi + dot_z_eq)) + (- rn + x_rn + prem)/4 + e_ls;
% Setting e1 equal to 0 reduces the equation to the simple UIP

%% B. UIP when the exchange rate is managed to meet the inflation objective
% ls = e1*ls_tar + (1-e1)*(ls{+1} + (x_rn - rn + prem)/4 + e_ls);

% e1 captures either exchange rate persistency or central bank's FOREX interventions; 
% e1 varies between zero to 0.9 (tight control of the exchange rate); 
p.e1 = 0.4;

%% 4. Monetary policy reaction function (a forward-looking Taylor rule)

%% A. The standard rule
% rn = f1*rn{-1} + (1-f1)*(rn_neutral + f2*(dot4_cpi{+4} - target{+4}) + f3*lgdp_gap) + e_rn;

 %policy persistence; 
% f1 varies from 0 (no persistence) to 0.8 ("wait and see" policy)
p.f1 = 0.7;

% policy reactiveness: the weight put on inflation by the policy maker); 
% f2 has no upper limit but must be always higher then 0 (the Taylor principle)
p.f2 = 0.5;

% policy reactiveness: the weight put on the output gap by the policy maker); 
% f3 has no upper limit but must be always higher then 0
p.f3 = 0.5;

%% B. The rule modified for imperfect control over the domestic money market
%% (use if the central bank stabilizes the exchange rate by FOREX interventions)

% rn = g1*(4*(ls{+1} - ls) + x_rn + prem) + (1-g1)*(f1*rn{-1} + (1-f1)*(rn_neutral + f2*(dot4_cpi{+4} - target{+4}) + f3*lgdp_gap)) + e_rn;

% degree to which the central bank does not control domestic money market
p.g1 = 0;

%% 5. Speed of convergence of selected variables to their trend values.
% Used for risk ppremium, trends, foreign variables and world commodity prices, 

% persistent shock to risk premium
% shock_prem = h0*shock_prem{-1} + e_prem;
p.h0 = 0.5;

% persistence in onvergence of trend variables to their steady-state levels
% applies for dot_gdp_eq, dot_z_eq, rr_eq and x_rr_eq
% example:
% dot_z_eq = h1*dot_z_eq{-1} + (1-h1)*ss_dot_z_eq + e_dot_z_eq and
% dot_gdp_eq = h1*dot_gdp_eq{-1} + (1-h1)*ss_dot_gdp_eq + e_dot_gdp_eq;
p.h1 = 0.8;

% persistence in foreign GDP 
% lx_gdp_gap = h2*lx_gdp_gap{-1} + e_lx_gdp_gap;
p.h2 = 0.8;

% persistence in foreign interest rates and inflation;
% example:
%x_rn = h3*x_rn{-1} + (1-h3)*(x_rr_eq + dot_x_cpi) + e_x_rn;
p.h3 = 0.8;

% persistence in cross exchange rate and world food and oil prices
% example:
%dot_wfood = h4*dot_wfood{-1} + (1-h3)*(ss_dot_x_cpi) + e_dot_wfood;
p.h4 = 0.1;

% Speed of inflation target adjustment to the medium-term target (higher
% values mean slower adjustment)
% target = t1*target{-1} + (1-t1)*target_ss + e_target;
p.t1 = 0.5;

%% The inflation target and other observed economic trends
% These "steady-state" values are all calibrated

% Foreign trend inflation or inflation target
p.ss_dot_x_cpi = 2;

% Trend level of domestic real interest rate 
p.ss_rr_eq = 0.5;

% Trend change in the real ER (negative number = real appreciation)
p.ss_dot_z_eq = -1;

% Potential output growth
p.ss_dot_gdp_eq = 2;

% Trend level of foreign real interest rate
p.ss_x_rr_eq = 0.5;

% Domestic inflation target
p.target_ss = 2;

%% Model solving--a brief description of commands
% Command 'model' reads the text file 'model.mod' (contains the model's
% equations), assigns the parameters and trend values preset in the database
% 'p' (see readmodel) and transforms the model for the matrix algebra. 
% Transformed model is written in the object 'm'. 
p.nonlinear = false;
m = model('model.model','linear=',true,'assign',p);

% Command 'solve' takes the model saved in object 'm' and solves the model
% for its reduced form (Blanchard-Kahn algorithm). The reduced form is again  
% written in the object 'm'   
m = solve(m);

% Command 'sstate' takes the transformed model in object 'm', calculates the model's
% steady-state and writes everything back in the object 'm'. Typing 'mss' in
% Matlab command window provides the steady-state values.
m = sstate(m,'growth',true,'MaxFunEvals',2000);
mss = get(m,'sstate');
p = dbextend(p,mss);

% Solve model as non-linear using steady-state calculated for linear one
p.nonlinear = true;
m = model('model.model','linear=',false,'assign',p);
m = solve(m);

%% Check steady state
[flag,discrep,eqtn] = chksstate(m,'error',false);

if ~flag
  error('Equation fails to hold in steady state: "%s"\n', eqtn{:});
end